﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.OpenGl;
using PocketEngine.Engine;
using PocketEngine.Effects;
using PocketEngine.Materials;
using PocketEngine.Images;
namespace PocketEngine.RenderTargets
{
    public class pRenderTarget
    {
        public int Buffer
        {
            get
            {
                return mBuf;
            }
            set
            {
                mBuf = value;
            }
        }
        private int mBuf = 0;
        public pRenderTarget()
        {
            Gl.glGenFramebuffersEXT(1, out mBuf);

        }
        public void Bind()
        {
            Gl.glBindFramebufferEXT(Gl.GL_FRAMEBUFFER_EXT,mBuf);
        }
        public void Unbind()
        {
            Gl.glBindFramebufferEXT(Gl.GL_FRAMEBUFFER_EXT, 0);
        }
        public void AttachImage(pImage image)
        {
            Gl.glFramebufferTexture2DEXT(Gl.GL_FRAMEBUFFER_EXT, Gl.GL_COLOR_ATTACHMENT0_EXT, Gl.GL_TEXTURE_2D, image.GLTexture, 0);
            CheckStatus();
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
        }
        public void CheckStatus()
        {
            int status = Gl.glCheckFramebufferStatusEXT(Gl.GL_FRAMEBUFFER_EXT);
            if (status != Gl.GL_FRAMEBUFFER_COMPLETE_EXT)
            {
                pEngine.Err("FrameBuffer incomplete.", "pRenderTarget.CheckStatus");
            }
        }
    }
}
